package com.masterclient.app.modules.cart.holder;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.masterclient.app.modules.cart.adapter.ShopCartAdapterListener;
import com.masterclient.app.modules.cart.beans.ProductsItemBean;
import com.masterclient.app.modules.cart.beans.PropertiesBean;
import com.masterclient.app.utils.CommonUtil;
import com.masterclient.app.widget.PriceIntegralLayout;
import com.masterclient.app.widget.TagTextView;

import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

public class DisableItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private CheckBox checkBox;
    private ImageView ivCanNotCheck;
    private ImageView ivGood;
    private TagTextView tvTitle;
    private TextView tvSkus;
    private TextView tvMoneyFlag;
    private PriceIntegralLayout llPrice;
    private LinearLayout llChangeNum;
    private View viewDivide;
    private View viewFans;
    private TextView tvFansDes;

    private Context mContext;
    private ShopCartAdapterListener mListener;
    private ProductsItemBean productsItemBean;
    private int position;

    public DisableItemHolder(ViewGroup itemView, ShopCartAdapterListener listener) {
        super(LayoutInflater.from(itemView.getContext()).inflate(R.layout.view_item_cart_good, itemView, false));
        mContext = itemView.getContext();
        mListener = listener;
        LinearLayout viewItem = this.itemView.findViewById(R.id.view_item);
        checkBox = this.itemView.findViewById(R.id.checkBox);
        ivCanNotCheck = this.itemView.findViewById(R.id.iv_cannot_check);
        ivGood = this.itemView.findViewById(R.id.iv_good);
        tvTitle = this.itemView.findViewById(R.id.tv_title);
        tvSkus = this.itemView.findViewById(R.id.tv_skus);
//        tvMoneyFlag = this.itemView.findViewById(R.id.money_flag);
        llPrice = this.itemView.findViewById(R.id.ll_price);
        llChangeNum = this.itemView.findViewById(R.id.ll_change_num);
        TextView tvCollect = this.itemView.findViewById(R.id.tv_collect);
        TextView tvDelete = this.itemView.findViewById(R.id.tv_delete);
        viewDivide = this.itemView.findViewById(R.id.v_divide);
        viewFans = this.itemView.findViewById(R.id.fl_fans);
        tvFansDes = this.itemView.findViewById(R.id.tv_fans_des);

        viewItem.setOnClickListener(this);
        tvCollect.setOnClickListener(this);
        tvDelete.setOnClickListener(this);
    }

    public void onBindViewHolder(ProductsItemBean itemBean, int position) {
        productsItemBean = itemBean;
        this.position = position;

        checkBox.setVisibility(View.GONE);
        llChangeNum.setVisibility(View.GONE);
        ivCanNotCheck.setVisibility(View.VISIBLE);

        ViewTransformUtil.glideImageView(mContext, itemBean.image, ivGood,
                new RoundedCornersTransformation(ScreenUtils.dp2px(4), 0),
                R.mipmap.defaults, ScreenUtils.dp2px(107), ScreenUtils.dp2px(107));

        tvTitle.setText(itemBean.title);
        tvTitle.setTextColor(Color.parseColor("#999FAA"));

        //sku
        List<PropertiesBean> listProperty = itemBean.properties;
        StringBuilder sku = new StringBuilder();
        if (listProperty != null) {
            sku.append("规格：");
            for (PropertiesBean property : listProperty) {
                sku.append("“")
                        .append(property.aliasName)
                        .append("” ");
            }
        }
        tvSkus.setText(sku);
//        tvPrice.setText(itemBean.price);
//        tvPrice.setTextColor(Color.parseColor("#999FAA"));
//        tvMoneyFlag.setTextColor(Color.parseColor("#999FAA"));
        llPrice.setVisibility(View.GONE);
//        tvMoneyFlag.setVisibility(View.GONE);
        if (itemBean.showLine) {
            viewDivide.setVisibility(View.VISIBLE);
        } else {
            viewDivide.setVisibility(View.GONE);
        }

        if ("3".equals(itemBean.productType) && !TextUtils.isEmpty(itemBean.text)){
            viewFans.setVisibility(View.VISIBLE);
            tvFansDes.setText(itemBean.text);
            viewFans.setOnClickListener(view -> {
                CommonUtil.goAnyWhere(mContext, itemBean.activityUrl,"",  "", "", "");
            });
        }else {
            viewFans.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.view_item:
                mListener.onGoIntoGoodDetails(productsItemBean.productMainId, position);
                break;
            case R.id.tv_collect:
                mListener.onCollectGoods(productsItemBean.cartId);
                break;
            case R.id.tv_delete:
                mListener.onDeleteGoods(productsItemBean.cartId);
                break;
        }
    }
}
